package com.spzx.product.controller;

import com.alibaba.cloud.nacos.discovery.reactive.NacosReactiveDiscoveryClient;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import com.spzx.domain.Brand;
import com.spzx.product.service.IBrandService;
import com.spzx.vo.Result;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

@RestController
@RequestMapping("/brand")
public class BrandController {

    @Autowired
    private IBrandService brandService;

    @Operation(summary = "新增品牌")
    @PostMapping
    public Result addBrand(@RequestBody Brand brand) {
        brandService.saveOrUpdate(brand);
        return Result.build("", 1, "");
    }

    @Operation(summary = "更新品牌")
    @PutMapping
    public Result update(@RequestBody Brand brand) {
        brandService.saveOrUpdate(brand);
        return Result.build("", 1, "");
    }

    @Operation(summary = "获取详细信息")
    @GetMapping("/{id}")
    public Result getBrand(@PathVariable Long id) {
        return Result.build(brandService.getById(id), 1, "");
    }


    @Operation(summary = "分页查询")
    @GetMapping("/findPage/{page}/{limit}")
    public Result<PageInfo<Brand>> list(@PathVariable Integer page, @PathVariable Integer limit, Brand brand) {
        Page<Brand> brandPage = new Page<>(page,limit);
        String name = brand.getName();
        PageInfo<Brand> pageList = brandService.pageList(brandPage,name);
        return Result.build(pageList,1,"");
    }


    @Operation(summary = "删除品牌")
    @DeleteMapping("/{ids}")
    public Result removeBrand(@PathVariable Long[] ids) {
//        brandService.removeBrandByIds(ids);
        brandService.removeByIds(Arrays.asList(ids));
        return Result.build("", 1, "");
    }


}
